package com.gxa.modules.community.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.modules.community.entity.*;
import com.gxa.modules.community.service.ParkingSpaceService;
import com.gxa.modules.community.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "车位管理")
public class ParkingSpaceController {
    @Autowired
    private ParkingSpaceService parkingSpaceService;

    @ApiOperation("车位管理 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value ="当前是第几页",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value ="每页显示多少条",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "carstateId",value ="车位状态",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "carparkingId",value ="车位编号",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "serialNo",value ="人的身份id",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "communityId",value ="小区id",dataType ="int"),
    }
    )
    @GetMapping("/parking/list")
    public Result<List<ParkingSpace>> list01(@RequestParam @ApiIgnore Map<String,Object> params){
        Result<List<ParkingSpace>> r=Result.success();
        String page1=(String) params.get("page");
        String limit1=(String)params.get("limit");
        Integer page=Integer.valueOf(page1);
        Integer limit=Integer.valueOf(limit1);
        try {
            PageHelper.startPage(page,limit);
            List<ParkingSpace> parkingSpaces = this.parkingSpaceService.queryByPage(params);
            PageInfo<ParkingSpace> pageInfo = new PageInfo<>(parkingSpaces);
            long total = (int) pageInfo.getTotal();
            r=Result.success(parkingSpaces,total);
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("持有人 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value ="当前是第几页",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value ="每页显示多少条",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "userId",value ="持有人的id(带条件的查询——可不传)",dataType ="String"),
    }
    )
    @GetMapping("/person/list")
    public Result<List<Person>> list02(@RequestParam @ApiIgnore Map<String,Object> params){
        Result<List<Person>> r=Result.success();
        String page1=(String) params.get("page");
        String limit1=(String)params.get("limit");
        Integer page=Integer.valueOf(page1);
        Integer limit=Integer.valueOf(limit1);
        try {
            PageHelper.startPage(page,limit);
            List<Person> personList = this.parkingSpaceService.queryPerson(params);
            PageInfo<Person> pageInfo = new PageInfo<>(personList);
            long total = (int) pageInfo.getTotal();
            r=Result.success(personList,total);
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("车位状态下拉 查询")
    @GetMapping("/carstate/list")
    public Result<List<ParkingType>> list03(){
        Result<List<ParkingType>> r=Result.success();
        try {
            List<ParkingType> parkingTypes = this.parkingSpaceService.queryParkingType();
            r=Result.success(parkingTypes);
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("车位管理 添加")
    @PostMapping("/parking/add")
    public Result<List<SpaceE>> parkingSpaceAdd(@RequestBody SpaceE spaceE){

        System.out.println(spaceE);
        Result<List<SpaceE>> r=Result.success();
        try {
            this.parkingSpaceService.parkingSpaceAdd(spaceE);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("车位管理 修改")
    @PutMapping("/parking/update")
    public Result<List<SpaceE>> parkingSpaceUpdate(@RequestBody SpaceE spaceE){

        System.out.println(spaceE);
        Result<List<SpaceE>> r=Result.success();
        try {
            this.parkingSpaceService.parkingSpaceUpdate(spaceE);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("车位管理 删除")
    @DeleteMapping("/parking/delete")
    public Result<List<SpaceE>> regionDelete(Integer id){
        Result<List<SpaceE>> r=Result.success();
        try {
            this.parkingSpaceService.parkingSpaceDelete(id);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
    @ApiOperation("区域管理 批量删除")
    @DeleteMapping("/parking/ids/delete")
    public Result<List<SpaceE>> regionDeleteIds(@RequestBody List<Integer> ids){
        Result<List<SpaceE>> r=Result.success();
        try {
            this.parkingSpaceService.regionDeleteIds(ids);
            r=Result.success();
        }catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }
}
